//
//  CLEmoiconCollectionViewCell.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/19.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit

class CLEmoiconCollectionViewCell: UICollectionViewCell {
    
    
    // MARK:  懒加载控件
    
    //删除按钮
    private lazy var deleteButton:UIButton = {
    
        let button = UIButton()
        //删除按钮点击事件
        button.addTarget(self, action: "deleteButtonAction", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return button
    }()
    //表情按钮模型
    private lazy var emoiconButtons:[CLEmoiconButton] = [CLEmoiconButton]()
    //绑定数据模型
    var emoiconModel:[CLEmoiconModel]? {
        didSet {
            guard let model = emoiconModel else {
                return
            }
            //隐藏全部按钮,单元格复用,在有数据的时候再去显示
            for value in emoiconButtons {
                //隐藏表情按钮
                value.hidden = true
            }
            //给表情按钮绑定数据
            for (i,value) in model.enumerate() {
                //获取表情按钮
                let button = emoiconButtons[i]
                //设置模型 为模型赋值  就会走 didSet方法  在抽取的emoiconButton类中实现
                button.emoiconModel = value
                //以下代码已抽取
//                //设置数据显示按钮
//                button.hidden = false
//                if value.type == "0" {
//                    button.setImage(UIImage(named: value.path!), forState: .Normal)
//                    button.setTitle(nil, forState: .Normal)
//                }else {
//                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
//                    button.setImage(nil, forState: .Normal)
//                }
            }
        }
    }
    
    
    //构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    //XIB加载调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //布局界面
    private func setUpUI(){
        contentView.addSubview(deleteButton)
        addChildButton()
    }
    
    //添加20个表情按钮
    private func addChildButton() {
        //循环遍历创建
        for _ in 0..<20 {
            let button = CLEmoiconButton()
            //点击表情添加事件
            button.addTarget(self , action: "emoiconButtonClickAction:", forControlEvents: .TouchUpInside)
//            button.backgroundColor = UIColor.yellowColor()
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            addSubview(button)
            //添加到数组
            emoiconButtons.append(button)
        }
    }
    
    
    //设置子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算宽度
        let itemWidth = width / 7
        //计算高度
        let itemHeight = height / 3
        
        for (i,button) in emoiconButtons.enumerate() {
            //列数的索引
            let cols = i % 7
            //行数的索引
            let rows = i / 7
            //按钮的尺寸
            button.size = CGSize(width: itemWidth, height: itemHeight)
            //设置布局
            button.x = CGFloat(cols) * itemWidth
            button.y = CGFloat(rows) * itemHeight + 5
        }
        //删除按钮
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemWidth
        deleteButton.size = CGSize(width: itemWidth, height: itemWidth)
    }
    // MARK:  删除按钮点击事件处理
    @objc private func deleteButtonAction(){
        print("删除按钮")
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelecteDeleteButtonNotification, object: nil)
        
        
    }
    // MARK:  表情按钮点击事件
    @objc private func emoiconButtonClickAction(button:CLEmoiconButton){
        
     //利用通知 传递模型数据
       
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelecteEmoticonNotification, object: button.emoiconModel!)
//         print(button.emoiconModel!)
    }
    
    
}
